package arch.dao.impl.archive;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.springframework.jdbc.core.RowMapper;

import arch.domain.archive.Archive;
import arch.domain.archive.ArchiveNumber;
import arch.domain.archive.BoxNumber;
import arch.domain.archive.Category;
import arch.domain.archive.OfficialNumber;
import arch.domain.archive.OfficialWord;
import arch.domain.archive.Retention;
import arch.domain.archive.Security;
import arch.domain.user.Dept;

/**
 * 
 * @author <a mailto="hwangsyin@gmail.com">King</a>
 *
 */
public class ArchiveRowMapper implements RowMapper {

	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		Archive archive = new Archive();
		
		archive.setId(rs.getString("id"));
		archive.setTitle(rs.getString("title"));
		archive.setResponsible(rs.getString("responsible"));
		archive.setFrom(rs.getString("from"));
		archive.setTo(rs.getString("to"));
		String creatorId = rs.getString("creator_id");
		if(creatorId != null) {
			archive.setCreator(new Dept());
			archive.getCreator().setDeptCode(creatorId);
			archive.getCreator().setDeptName(rs.getString("creator_name"));
		}
		archive.setQualifiedNumber(rs.getString("qualified_number"));
		archive.setYear(rs.getString("year"));
		int securityId = rs.getInt("security_id");
		if(securityId > 0) {
			archive.setSecurity(new Security());
			archive.getSecurity().setId(securityId);
		}
		int officialWordId = rs.getInt("on_key_id");
		if(officialWordId > 0) {
			OfficialWord ow = new OfficialWord();
			ow.setRank(rs.getInt("on_key_rank"));
			ow.setId(officialWordId);
			OfficialNumber on = new OfficialNumber();
			on.setOfficialWord(ow);
			try {
				on.setYear(Integer.valueOf(rs.getString("on_year")));
			} catch(Throwable e) {
				
			}
			on.setSerial(rs.getInt("on_serial"));
			archive.setOfficialNumber(on);
		}
		int retentionId = rs.getInt("retention_id");
		if(retentionId > 0) {
			archive.setRetention(new Retention());
			archive.getRetention().setId(retentionId);
		}
		java.sql.Date cTime = rs.getDate("confirmation_time");
		if(cTime != null) {
			archive.setConfirmationTime(new Date(cTime.getTime()));
		}
		String openString = rs.getString("open");
		archive.setOpenString(openString);
		java.sql.Date crTime = rs.getDate("create_time");
		if(crTime != null) {
			archive.setCreateTime(new Date(crTime.getTime()));
		}
		long serial = rs.getLong("an_serial");
		if(serial > 0) {
			archive.setArchiveNumber(new ArchiveNumber());
			archive.getArchiveNumber().setSerial(serial);
		}
		int boxNumberSerial = rs.getInt("bn_serial");
		if(boxNumberSerial > 0) {
			archive.setBoxNumber(new BoxNumber());
			archive.getBoxNumber().setSerial(boxNumberSerial);
		}
		int categoryId = rs.getInt("category_id");
		if(categoryId > 0) {
			archive.setCategory(new Category());
			archive.getCategory().setId(categoryId);
		}
		archive.setMedium(rs.getString("medium"));
		archive.setAssociation(rs.getString("association"));
		archive.setPages(rs.getInt("pages"));
		
		return archive;
	}

}
